﻿// CreateWindow.cpp : 此文件包含 "main" 函数。程序执行将在此处开始并结束。
//

#include <iostream>
#include <Windows.h>
#include <string>
#include <sstream>
#include <ostream>
#include "WindowMsgMap.h"


#if 0
LRESULT CALLBACK WndProc(HWND hwnd, UINT msg, WPARAM wParam, LPARAM lParam) {

	/*static WindowMsgMap msgMap;
	auto str = msgMap(msg, lParam, wParam);
	OutputDebugString(str.c_str());*/

	switch (msg)
	{
	case WM_CLOSE:
		PostQuitMessage(123);
		break;
	case WM_KEYDOWN:
		if (wParam == 'A')
		{
			SetWindowText(hwnd, "Keydown");
		}
		break;
	case WM_KEYUP:
		if (wParam == 'A')
		{
			SetWindowText(hwnd, "Key Up1111");
		}
		break;
	case WM_CHAR:
	{
		static std::string title;
		title.push_back((char)wParam);
		SetWindowText(hwnd, title.c_str());
	}
	break;
	case WM_LBUTTONDOWN:
	{
		POINTS pt = MAKEPOINTS(lParam);
		std::ostringstream oss;
		oss << "(" << pt.x << "," << pt.y << ")";
		SetWindowText(hwnd, oss.str().c_str());
	}
	break;
	default:
		break;
	}
	return DefWindowProc(hwnd, msg, wParam, lParam);
}
#endif // 0


#include "App.h"

int CALLBACK WinMain(
	HINSTANCE hInstance,
	HINSTANCE hPrevInstance,
	LPSTR     lpCmdLine,
	int       nCmdShow)
{
	try
	{
		App app;
		app.GO();
	}
	catch (const std::exception& e)
	{
		MessageBoxA(nullptr, e.what(), "错误", MB_OK);
	}


#if 0

	try
	{
		OutputDebugString("hello world!");
		//throw MY_EXCEPTION();
		//throw std::runtime_error("xxoo hahah");
		throw "你大爷";
	}
	catch (const MyException& e)
	{
		MessageBox(nullptr, e.what(), "MyException Title", MB_OK | MB_ICONEXCLAMATION);
	}
	catch (const std::exception& e) {
		MessageBox(nullptr, e.what(), "标准 exception", MB_OK | MB_ICONEXCLAMATION);
	}
	catch (...) {
		MessageBox(nullptr, "my unknow", "其他异常", MB_OK | MB_ICONEXCLAMATION);
	}

	const auto pClassName = "hw3dbutts";
	// register window class
	WNDCLASSEX wc = { 0 };
	wc.cbSize = sizeof(wc);
	wc.style = CS_OWNDC;
	wc.lpfnWndProc = WndProc;
	wc.cbClsExtra = 0;
	wc.cbWndExtra = 0;
	wc.hInstance = hInstance;
	wc.hIcon = nullptr;
	wc.hCursor = nullptr;
	wc.hbrBackground = nullptr;
	wc.lpszMenuName = nullptr;
	wc.lpszClassName = pClassName;
	wc.hIconSm = nullptr;
	RegisterClassEx(&wc);


	DWORD windowStyle = WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU;
	BOOL hasMenu = false;

	const int width = 640;
	const int height = 480;
	RECT rect;
	rect.left = rect.top = 100;
	rect.right = rect.left + width;
	rect.bottom = rect.top + height;
	if (AdjustWindowRect(&rect, windowStyle, hasMenu) == 0) {
		throw "adjust window rect failed!";
	}

	std::ostringstream oss;
	int newWidth = rect.right - rect.left;
	int newHeight = rect.bottom - rect.top;
	oss << "left:" << rect.left << ",right:" << rect.right << ",top:" << rect.top << ",bottom:" << rect.bottom << ",newWidth:" << newWidth << ", newHeight:" << newHeight << std::endl;
	OutputDebugString(oss.str().c_str());


	// create window instance
	HWND hWnd = CreateWindowEx(
		0, pClassName,
		"Happy Hard Window",
		WS_CAPTION | WS_MINIMIZEBOX | WS_SYSMENU,
		200, 200, 640, 480,
		nullptr, nullptr, hInstance, nullptr
	);
	// show the damn window
	ShowWindow(hWnd, SW_SHOW);

	// msg loop
	MSG msg;
	BOOL result;
	while ((result = GetMessage(&msg, nullptr, 0, 0)) > 0)
	{
		TranslateMessage(&msg);
		DispatchMessage(&msg);
	}

	if (result == -1) {
		return -1;
	}
	else {
		return msg.wParam;
	}
#endif // 0
}
